package com.dashu.device.service.CQ;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dashu.base.bean.device.CQ.req.CQDevicePicAddReq;
import com.dashu.base.bean.device.CQ.req.CQDevicePicDelReq;
import com.dashu.base.bean.device.CQ.req.CQDevicePicUpdReq;
import com.dashu.base.bean.device.DeviceBase;
import com.dashu.base.utils.ToEntityAnStr;
import com.dashu.base.vo.*;
import com.dashu.device.api.CQ.CQExternalApi;
import com.dashu.device.bean.CQ.CQBaseReq;
import com.dashu.device.bean.CQ.OpenDoorStatusBean;
import com.dashu.device.bean.CQ.CQAddGroupBean;
import com.dashu.device.dto.*;
import com.dashu.device.util.CQWriteUtil;
import com.dashu.device.vo.RestartReqVO;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.dashu.base.enums.device.GroupType.WHITE_NAME;
import static com.dashu.device.container.CQContainer.CQonlineStr;
import static com.dashu.device.tools.CQMark.*;

@Slf4j
@Service
public class CQInsideExternalService<T> implements CQExternalApi<T> {

    /**
     * 创建用户
     */
    @Override
    public DevicePicAddRespDTO<T> addUser(DevicePicAddVO<T> req) {
        log.info("CQAdapter2.addPic:" + req.getDeviceUUID() + "设备");
        JSONObject Json = JSONObject.parseObject(req.getResp());
        CQDevicePicAddReq picAddReq = JSON.toJavaObject(Json, CQDevicePicAddReq.class);
        ChannelHandlerContext ctx = CQonlineStr.get(req.getDeviceUUID());
//        picAddReq.setFacePhotoUrl(picAddReq.getFacePhotoUrl());
//        picAddReq.setUserName(picAddReq.getUserName());
        picAddReq.setGroupId(WHITE_NAME.toString());
        picAddReq.setUserId(req.getIden().toString() + "_" + req.getUserId());
        if (ctx != null) {
            CQAddGroupBean addGroupBean = new CQAddGroupBean();
            addGroupBean.setGroupId(GROUP_NAME);
            CQBaseReq cqBaseReq = CQWriteUtil.request(ADDUSER, req.getDeviceUUID(), picAddReq);
            CQWriteUtil.send(ctx, ToEntityAnStr.obj2String(cqBaseReq));
        }
        DevicePicAddRespDTO<T> addResp = new DevicePicAddRespDTO<>();
        addResp.setMsg("添加成功");
        log.info("CQAdapter2.addPic:");
        return addResp;
    }

    @Override
    public DevicePicDelRespDTO<T> delPic(DevicePicDelReqVO<T> req) {
        log.info("CQAdapter2.delPic:" + req.getDeviceUUID() + "设备");
        ChannelHandlerContext ctx = CQonlineStr.get(req.getDeviceUUID());
        JSONObject Json = JSONObject.parseObject(req.getResp());
        CQDevicePicDelReq picDelReq = JSON.toJavaObject(Json, CQDevicePicDelReq.class);
        picDelReq.setGroupId(WHITE_NAME.toString());
        picDelReq.setUserId(req.getIden().toString() + "_" + picDelReq.getUserId());
        CQBaseReq cqBaseReq = CQWriteUtil.request(DELUSER, req.getDeviceUUID(), picDelReq);
        CQWriteUtil.send(ctx, ToEntityAnStr.obj2String(cqBaseReq));
        DevicePicDelRespDTO<T> delResp = new DevicePicDelRespDTO<>();
        delResp.setMsg("删除用户");
        log.info("CQAdapte2.delPic:" + delResp.toString());
        return delResp;
    }

    @Override
    public DevicePicUpdRespDTO<T> updPic(DevicePicUpdReqVO<T> req) {
        log.info("CQAdapter2.updPic:" + req.getDeviceUUID() + "设备");
        ChannelHandlerContext ctx = CQonlineStr.get(req.getDeviceUUID());
        JSONObject Json = JSONObject.parseObject(req.getResp());
        CQDevicePicUpdReq picAddReq = JSON.toJavaObject(Json, CQDevicePicUpdReq.class);
//        picAddReq.setFacePhotoUrl(picAddReq.getFacePhotoUrl());
//        picAddReq.setUserName(picAddReq.getUserName());
        picAddReq.setGroupId(WHITE_NAME.toString());
        picAddReq.setUserId(req.getIden().toString() + "_" + picAddReq.getUserId());
        CQBaseReq cqBaseReq = CQWriteUtil.request(UPDUSER, req.getDeviceUUID(), picAddReq);
        CQWriteUtil.send(ctx, ToEntityAnStr.obj2String(cqBaseReq));
        DevicePicUpdRespDTO<T> updResp = new DevicePicUpdRespDTO<>();
        updResp.setMsg("修改用户");
        log.info("CQAdapter2.updPic:" + updResp.toString());
        return updResp;
    }

    @Override
    public DeviceOpenDoorRespDTO<T> openDoor(DeviceOpenDoorReqVO<T> req) {
        log.info("CQAdapter.openDoor:" + req.toString());
        ChannelHandlerContext ctx = CQonlineStr.get(req.getClientUUID());
        OpenDoorStatusBean cqOpenDoorBean = new OpenDoorStatusBean();
        //将图片转base64
        DeviceBase deviceBase = req.getBeans();
        CQBaseReq cqBaseReq = CQWriteUtil.request(REMOTEOPEN, deviceBase.getDeviceUUID(), "");
        CQWriteUtil.send(ctx, ToEntityAnStr.obj2String(cqBaseReq));

        DeviceOpenDoorRespDTO<T> doorResp = new DeviceOpenDoorRespDTO<>();
        doorResp.setClientUUID(req.getClientUUID());
        doorResp.setMessageId(req.getMessageId());
        doorResp.setT(req.getT());
        doorResp.setStatus(cqOpenDoorBean.getStatus());
        doorResp.setMsg(cqOpenDoorBean.getMsg());
        doorResp.setMsg_en(cqOpenDoorBean.getMsg_en());
        doorResp.setModuleType(req.getModuleType());

        log.info("CQAdapter2.openDoor:" + doorResp.toString());
        return doorResp;
    }

    @Override
    public DeviceExistUserRespDTO<T> existUser(DeviceExistUserReqVO<T> req) {
        log.info("CQAdapter2.delPic:" + req.getBeans().getDeviceUUID() + "设备");
        ChannelHandlerContext ctx = CQonlineStr.get(req.getDeviceUUID());
        JSONObject Json = JSONObject.parseObject(req.getResp());
        CQDevicePicDelReq picDelReq = JSON.toJavaObject(Json, CQDevicePicDelReq.class);
        picDelReq.setGroupId(WHITE_NAME.toString());
        picDelReq.setUserId(req.getIden().toString() + "_" + picDelReq.getUserId());
        CQBaseReq cqBaseReq = CQWriteUtil.request(EXIST_USER, req.getDeviceUUID(), picDelReq);
        CQWriteUtil.send(ctx, ToEntityAnStr.obj2String(cqBaseReq));
        DeviceExistUserRespDTO<T> delResp = new DeviceExistUserRespDTO<>();
        delResp.setClientUUID(req.getDeviceUUID());
        log.info("CQAdapte2.delPic:" + delResp.toString());
        return delResp;
    }

    @Override
    public boolean restart(RestartReqVO<T> req) {
        ChannelHandlerContext ctx = CQonlineStr.get(req.getSn());
        CQBaseReq cqBaseReq = CQWriteUtil.request(RESTART, req.getSn(), null);
        CQWriteUtil.send(ctx, ToEntityAnStr.obj2String(cqBaseReq));

        return true;
    }


}
